package com.zqxq.lottery.api.controller;

import com.google.code.kaptcha.Producer;
import com.zqxq.lottery.api.utils.ValidateCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 苦智
 * @date 2018/10/15 11:08
 * @description 验证接口
 * @ClassName ValidateController
 */
@RestController
@RequestMapping("/validate")
@Api(value = "图形验证码",tags = "图形验证码")
@Slf4j
public class ValidateController {
    @Autowired
    private StringRedisTemplate redisTemplate;


    @RequestMapping("/getImageCode")
    @ApiOperation(value="获取验证码图片接口", notes="获取验证码图片接口")
    public void getCheckCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应的类型格式为图片格式
        response.setContentType("image/jpeg");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(120,40,5,150);
        String code = vCode.getCode();
        log.info("图形验证码为：" + code);
        redisTemplate.opsForValue().set("code"+code.toUpperCase(),code,3, TimeUnit.MINUTES);
//        session.setAttribute("code" + code.toUpperCase(), code);
//        session.setMaxInactiveInterval(2*60); //过期时间两分钟
        vCode.write(response.getOutputStream());
    }

}
